﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace PropertyManagement.yuanGong
{
    public partial class editYuanGong2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sid = Request.QueryString["id"];
            if (!IsPostBack && sid != null && !sid.Equals(""))
            {
                BindTextBox(sid);
            }
        }
        string sphoto;
        private void BindTextBox(string sid)
        {
            int id;
            id = Int16.Parse(sid);
            string strsql = "select * from syuangong where id=" + id;

            mydata myda = new mydata();
            MySqlDataReader dread = myda.getDataReader(strsql);

            if (dread.HasRows)
            {
                dread.Read();
                sid = dread["id"].ToString();
                txtId.Text = sid;

                txtsname.Text = dread["sname"].ToString();
                txtpwd.Text = dread["pwd"].ToString();
                txtAddress.Text= dread["address"].ToString();
                RadioButtonList1.SelectedValue = dread["sex"].ToString();
                txtBirth.Text = dread["birth"].ToString();
                txtTel.Text = dread["tel"].ToString();
                txtWork.Text = dread["zhiwu"].ToString();
                sphoto =dread["photo"].ToString();
                Image1.ImageUrl = "~/picture/" + sphoto;
            }

            dread.Close();
            myda.closeAll();
        }

        public void UpdateFile(string sPicName)
        {
            this.FileUpload1.PostedFile.SaveAs(sPicName);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string sid, sname, pwd, address, sex, birth, tel, zhiwu, photo;

            sid = txtId.Text;
            sname = txtsname.Text;
            pwd = txtpwd.Text;
            address = txtAddress.Text;
            sex = RadioButtonList1.SelectedValue;
            birth = txtBirth.Text;
            tel = txtTel.Text;
            zhiwu = txtWork.Text;
            photo = sphoto;

            string sfilename = "";

            if (FileUpload1.HasFile)
            {
                //判断图片文件扩展名是不是jpg/gif

                //
                string str = FileUpload1.PostedFile.FileName;
                int p1 = str.LastIndexOf("\\");
                int n1 = str.Length - p1 - 1;
                sfilename = str.Substring(p1 + 1, n1);

                string sPicName = Server.MapPath("~/picture/") + sfilename;
                this.Image1.ImageUrl = "~/picture/" + str;
                UpdateFile(sPicName);
                //this.Image1.ImageUrl = sPicName;

                photo = sfilename;
            }
            else
            {
                photo = sphoto;
            }

            int id = Int16.Parse(sid);

            string strsql = "update syuangong set " +
                "sname='" + sname + "'," +
                "pwd='" + pwd + "'," +
                "address='" + address + "'," +
                "sex='" + sex + "'," +
                "birth='" + birth + "'," +
                "tel='" + tel + "'," +
                "zhiwu='" + zhiwu + "'," +
                "photo='" + photo + "'" +
                "where id=" + id;

            int n;
            mydata myda = new mydata();
            n = myda.execSql(strsql);
            if (n > 0)
            {
                Response.Write("<script language=javascript> alert('更新数据成功') </script>");
            }
            else
            {
                Response.Write("<script language=javascript> alert('更新数据失败') </script>");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("editYuanGong.aspx");
        }
    }
}